/*************************************************************************
	> File Name: test1.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/21 19:30
 ************************************************************************/

#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

#define THREAD_NAME 5
#define NAME_SIZE 64

int x = 10;

void show(const std::string &name)
{
    std::cout << name << ", pid:" << getpid() << ", x = " << x << '\n' << std::endl;
}

void *threadRun(void *args)
{
    const std::string name = (char*)args;
    while (1) {
        show(name);
        sleep(1);
    }
}
int main(int argc, char *argv[])
{
    pthread_t tid[THREAD_NAME];
    char name[NAME_SIZE];
    for (int i = 0; i < THREAD_NAME; ++i) {
        snprintf(name, sizeof name, "%s-%d", "thread", i);
        pthread_create(tid + i, nullptr, threadRun, (void*)name);
        sleep(1);
    }
    while (1) {
        std::cout << "Main thread pid:" << getpid() << '\n' << std::endl;
        sleep(3);
    }
    return 0;
}
